<?php


namespace app\mall\model\School;


use think\facade\Db;
use think\Model;

class SchoolModel extends Model
{

    /**
     * 数据表名
     * @var string
     */
    protected $name = "school";

    /**
     * 根据条件查询多条数据
     * @param $where
     * @return array
     * @throws
     */
    public static function getSchoolWhereMore($where=[])
    {
        $list = Db::name("school")->where($where)->select()->toArray();
        return $list;
    }

    /**
     * 统计总数
     * @param $where
     * @return int
     */
    public static function getSchoolCountWhere($where)
    {
        $count = Db::name("school")->where($where)->count();
        return $count;
    }

    /**
     * 分页查询
     * @param $page
     * @param $limit
     * @param $where
     * @return mixed
     * @throws
     */
    public static function getSchoolListWhere($page,$limit,$where = [],$field="s.*")
    {
        $start = ceil(($page-1)*$limit);
        $list = Db::name('school')
            ->alias('s')
            ->join('school_area sa','sa.id = s.area_id','left')
            ->field($field)
            ->where($where)
            ->limit($start,$limit)
            ->select()
            ->toArray();
        return $list;
    }

    /**
     * 添加返回ID
     * @param $data
     * @return int
     */
    public static function addSchool($data)
    {
        $id = Db::name('school')->insertGetId($data);
        return $id;
    }

    /**
     * 更新
     * @param $where
     * @param $save
     * @return int
     * @throws
     */
    public static function updateSchoolWhere($where,$save)
    {
        $flag = Db::name('school')->where($where)->update($save);
        return $flag;
    }

    /**
     * 删除
     * @param $where
     * @return int
     * @throws
     */
    public static function delSchoolWhere($where)
    {
        $flag = Db::name('school')->where($where)->delete();
        return $flag;
    }

}